from flask_sqlalchemy import SQLAlchemy
from datetime import datetime

db = SQLAlchemy()

class User(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    username = db.Column(db.String(80), unique=True, nullable=False)
    password = db.Column(db.String(256), nullable=False)
    role = db.Column(db.String(20), default='voter')
    is_anonymous = db.Column(db.Boolean, default=False)
    created_at = db.Column(db.DateTime, default=datetime.utcnow)

class Vote(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    user_id = db.Column(db.Integer, db.ForeignKey('user.id'), nullable=True)
    theme_id = db.Column(db.String(64), nullable=False)
    round_id = db.Column(db.String(64), nullable=False)
    option_ids = db.Column(db.String(256), nullable=False)  # 逗号分隔
    voted_at = db.Column(db.DateTime, default=datetime.utcnow)
    ip = db.Column(db.String(64), nullable=True)

    user = db.relationship('User', backref=db.backref('votes', lazy=True))
